//
//  AlbumListCell.swift
//  PhotoBrowser
//
//  Created by leotao on 16/5/29.
//  Copyright © 2016年 leotao. All rights reserved.
//

import UIKit
import SnapKit
import AssetsLibrary

class AlbumListCell: UITableViewCell {
    let iconImageView = UIImageView();
    let nameLabel = UILabel();
    let countLabel = UILabel();
    var group:ALAssetsGroup? {
        didSet {
            if group != nil {
                self.nameLabel.text = group!.valueForProperty(ALAssetsGroupPropertyName) as? String;
                
                let image = group!.posterImage().takeUnretainedValue();
                self.iconImageView.image = UIImage(CGImage: image);
                
                self.countLabel.text = "\(group!.numberOfAssets())";
            }
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier);
        
        self.contentView.addSubview(countLabel);
        self.contentView.addSubview(nameLabel);
        self.contentView.addSubview(iconImageView);
        
        self.nameLabel.text = "Name";
        self.countLabel.text = "5";
        self.iconImageView.backgroundColor = UIColor.orangeColor();
        
        self.nameLabel.snp_makeConstraints(closure: { (make) in
            make.left.equalTo(self.iconImageView.snp_right).offset(20);
            make.bottom.equalTo(self.iconImageView.snp_centerY);
        });
        
        self.countLabel.snp_makeConstraints { (make) in
            make.left.equalTo(self.nameLabel);
            make.top.equalTo(self.iconImageView.snp_centerY);
        };
    }
    
    func configureCell(image:UIImage, name:String, count:Int) {
        self.iconImageView.image = image;
        self.nameLabel.text = name;
        self.countLabel.text = String(count);
    }
    
    override func layoutSubviews() {
        super.layoutSubviews();
        
        let width = bounds.height * 0.8;
        let height = width;
        self.iconImageView.frame = CGRectMake(10, (bounds.height - height) * 0.5, width, height);
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
